import LCData from "../../GMDWeakNetGameBase/LCData";
import GameConn from "../../GMDWeakNetGameBase/GameConn";
import NSLoader from "../../GMDWeakNetGameBase/NSLoader";
import QWEvent from "../../GMDBase/QWEvent";
import ShareUtil from "./ShareUtil";

//好友管理器
export default class FriendMgr
{ 
    //根据openId获取邀请玩家属性
    //openIds openId队列
    //fields 字段列表
    //success 成功回调 success(res) 
    //fail 失败回调 fail()
    public static GetPlayerInfo(
        openIds:string[],
        fields:string[],
        success:Function,
        fail:Function = null
        )
    {
        var reList = []
        var flist = ShareUtil.FList
        var requestList = []
        for(var i=0;i<openIds.length;i++)
        { 
            var openId = openIds[i]
            var uinfo = flist[openId]
            if(uinfo)//好友列表中存在
            {
                var attrs:any = {oid:openId}
                if(uinfo.name) attrs.name = uinfo.name;
                if(uinfo.icon) attrs.icon = uinfo.icon;
                reList.push(attrs)
                requestList.push(openId)
            }else//好友列表中不存在
            {
                reList.push({oid:openId,none:1})
            }
        }
        
        if(requestList.length>0)
        {
            var nm = {
                n:"PlyInfo",
                oids:requestList,
                fields:fields
            }
            //向服务器发起请求
            var loader = GameConn.Singletion.CreateLoader(nm)
            loader.addEventListener(
                NSLoader.OnComplete,
                (evt:QWEvent)=>{
                    var data = evt.details
                    if(data.r!=0) {
                        if(fail) fail()
                        return
                    }

                    //合并返回结果
                    var ulist = data.list
                    for(var i=0;i<ulist.length;i++)
                    {
                        var svrInfo = ulist[i]//服务器下发的信息
                        for(var j=0;j<reList.length;j++)
                        {
                            var localInfo = reList[j]//本地返回信息
                            if(localInfo.oid == svrInfo.oid)
                            {
                                //服务器信息覆盖本地返回值
                                for(var fd in svrInfo) localInfo[fd] = svrInfo[fd]
                                break
                            }
                        }
                    }

                    success(reList)
                },
                this
            ) 
        }else
        {
            success(reList)
        }
    }
}
